<%@ page import="java.sql.Connection" %>
<%@ page import="com.example.javaee.DB" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %><%
    String username = request.getParameter("username");
    String password = request.getParameter("password");

    Connection connection = DB.getConnection();
    String sqlSelect = "SELECT username FROM db_user.user WHERE username = ?";
    String sqlInsert = "INSERT INTO db_user.user VALUES(NULL, ?, MD5(?), NULL, NULL)";
    PreparedStatement preparedStatementSelect = connection.prepareStatement(sqlSelect);
    preparedStatementSelect.setString(1, username);
    ResultSet resultSet = preparedStatementSelect.executeQuery();
    if (resultSet.next()) {
        request.setAttribute("message", "Username is already existed.");
        request.getRequestDispatcher("signup.jsp").forward(request, response);
    } else {
        PreparedStatement preparedStatementInsert = connection.prepareStatement(sqlInsert);
        preparedStatementInsert.setString(1, username);
        preparedStatementInsert.setString(2, password);
        preparedStatementInsert.executeUpdate();
        response.sendRedirect("index.jsp");
    } 
%>